<!--新增英雄组件-->
<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    width="40%">
    <el-form :model="form" ref="form" :rules="rules" label-width="100px">
      <el-form-item label="英雄名称" prop="heroName">
        <el-input v-model="form.heroName"/>
      </el-form-item>
      <el-form-item label="英雄类型" prop="heroType">
        <el-select v-model="form.heroType" clearable placeholder="英雄类型">
          <el-option
            v-for="(item,key) of options"
            :key="key"
            :label="item"
            :value="key">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="头像" prop="heroImg">
        <el-input v-model="form.heroImg"/>
      </el-form-item>
      <el-form-item label="英雄别称" prop="nickName">
        <el-input v-model="form.nickName"/>
      </el-form-item>
      <el-form-item label="技能" prop="skill">
        <el-input v-model="form.skill"/>
      </el-form-item>
      <el-form-item label="难度等级" prop="hardGrade">
        <el-input v-model="form.hardGrade"/>
      </el-form-item>
      <el-form-item label="技能等级" prop="skillGrade">
        <el-input v-model="form.skillGrade"/>
      </el-form-item>
      <el-form-item label="攻击等级" prop="attackGrade">
        <el-input v-model="form.attackGrade"/>
      </el-form-item>
      <el-form-item label="生存等级" prop="subsistGrade">
        <el-input v-model="form.subsistGrade"/>
      </el-form-item>
      <el-form-item label="背景图" prop="background">
        <el-input v-model="form.background"/>
      </el-form-item>
      <el-form-item label="皮肤" prop="skinList">
        <el-input v-model="form.skinList"/>
      </el-form-item>
      <el-form-item label="描述内容" prop="content">
        <el-input v-model="form.content"/>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="sure">确 定</el-button>
  </span>
  </el-dialog>
</template>

<script>
import { Add, Edit } from '@/api/hero'

export default {
  name: "add",
  data() {
    return {
      options: {
        1: '刺客',
        2: '法师',
        3: '坦克',
        4: '战士',
        5: '射手',
        6: '辅助',
        7: '刺客/战士',
        8: '刺客/法师',
        9: '坦克/战士',
        10: '坦克/法师'
      },
      title: '',
      form: {
        skillId: 1
      },
      rules: {
        name: [{
          message: '*必填项',
          required: true,
          trigger: ['change', 'blur']
        }]
      },
      dialogVisible: false,
      isEdit: false
    }
  },
  created() {
  },
  methods: {
    handleOpen(row) {
      this.dialogVisible = true
      if (row) {
        this.title = `编辑英雄`
        this.form = JSON.parse(JSON.stringify(row))
        this.isEdit = true
      } else {
        this.title = `新增英雄`
        Object.keys(this.form).forEach(key => {
          this.form[key] = null
        })
        this.isEdit = false
      }
    },
    sure() {
      if (this.isEdit) {
        Edit(this.form).then(res => {
          if (res.data.code === 200) {
            this.$emit('update')
            this.dialogVisible = false
          }
        })
      } else {
        Add(this.form).then(res => {
          if (res.data.code === 200) {
            this.$emit('update')
            this.dialogVisible = false
          }
        })
      }
    }
  }
}
</script>

<style scoped lang='scss'>

</style>
